@extends('admin.default')
@section('title', '科目添加')
@section('content')
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <div class="layui-form-item">
                        <label for="L_email" class="layui-form-label">
                            <span class="x-red">*</span>科目名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="name" name="name" autocomplete="off" class="layui-input" value="{{$subject->name}}"></div>
                        <div class="layui-form-mid layui-word-aux">
                            <span class="x-red">*</span>科目名必须大于2个字符</div></div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="add" lay-submit="">更新</button></div>
                    <input type="hidden" name="id" value="{{$subject->id}}">
                    <input type="hidden" name="_method" value="PUT">
                    {{csrf_field()}}
                </form>
            </div>
        </div>
        <script>
            layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;
                form.on('submit(add)',
                function(data) {
                    data = data.field;
                    //表单验证
                    if (data.name.length < 2) {
                        layer.tips('科目名必须大于1个字符', '#name', {
                            tips: [2, '#009688']
                        });
                        return false;
                    }
                    //console.log(data);
                    //发异步，把数据提交给php
                    $.ajax({
                        type: 'POST',
                        url: '{{route('admin.subject.update',$subject->id)}}',
                        data: data,
                        dataType: 'json',
                        success: function (data) {
                            if(data == 1){
                                layer.alert('更新成功', {
                                    icon: 1,
                                    skin: 'layui-layer-molv' //样式类名
                                    ,closeBtn: 0
                                }, function(){
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                    parent.location.reload();
                                });
                            } else{
                                layer.alert('更新失敗', {
                                    icon: 2,
                                    skin: 'layui-layer-molv' //样式类名
                                    ,closeBtn: 0
                                });
                            }
                        },error: function(res) {
                            var msg=JSON.parse(res.responseText);
                            layer.alert(msg.errors['name'][0], {
                                icon: 2,
                                title: '提示信息',
                                skin: 'layui-layer-molv' //样式类名
                                ,closeBtn: 0
                            });
                        }
                    });
                    return false;
                });

            });</script>
     @stop
